import _ from 'lodash'
import config from './config'

// ------------------------------------
// Constants
// ------------------------------------
const CHOUJIANG_START = 'CHOUJIANG_START'
const CHOUJIANG_CHANGE = 'CHOUJIANG_CHANGE'
const CHOUJIANG_STOP = 'CHOUJIANG_STOP'
const CHOUJIANG_INVALID = 'CHOUJIANG_INVALID'
const CHOUJIANG_ERROR = 'CHOUJIANG_ERROR'

// 定时器
let timer = null

// ------------------------------------
// Actions
// ------------------------------------
const choujiangError = (msg) => {
  return {
    type: CHOUJIANG_ERROR,
    payload: msg,
  }
}
const choujiangStart = () => {
  return {
    type: CHOUJIANG_START
  }
}
const choujiangChange = () => {
  return {
    type: CHOUJIANG_CHANGE
  }
}
const choujiangStop = () => {
  return {
    type: CHOUJIANG_STOP
  }
}
const lgInvalid = () => {
  return {
    type: CHOUJIANG_INVALID
  }
}

const lgStart = () => {
  return (dispatch, getState) => {
    if (timer) {
      return
    }

    if (getState().layoutState.nodes.length === 0) {
      return dispatch(choujiangError('土豪,奖品太多人太少呀.....'))
    }

    dispatch(choujiangStart())

    timer = setInterval(() => {
      dispatch(choujiangChange())
    }, 50)
  }
}

const lgStop = () => {
  return (dispatch, getState) => {
    if (!timer) {
      return
    }

    clearInterval(timer)
    timer = null

    dispatch(choujiangStop())
  }
}

const lg = (type) => {
  return (dispatch, getState) => {
    if (type === 1) {
      return dispatch(lgInvalid())
    }

    if (getState().layoutState.isRun) {
      return dispatch(lgStop())
    }

    return dispatch(lgStart())
  }
}

export const actions = {
  lg,
}

// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [CHOUJIANG_START] : (state) => {
    return {
      ...state,
      changeData: '',
      isRun: true,
      err: null,
    }
  },
  [CHOUJIANG_CHANGE] : (state) => {
    // 计算节点的数量
    let num = state.nodes.length-1;
    // 随机一个数字
    let randNum = _.random(0, num)
    // 得到一个随机的号码
    let cd = state.nodes[randNum]

    return {
      ...state,
      changeData: cd,
    }
  },
  [CHOUJIANG_STOP] : (state) => {
    // 停止以后需要把当前号码从号码池里面移出来,以免重复中奖
    let nodes = _.remove([...state.nodes], (v) => {
      return v !== state.changeData
    })

    // 判断是否是最后一个
    let isOver = state.isOver
    let sumNum = 0
    // 判断数量是否抽完了
    _.forEach(state.config.winConfig, (v) => {
      sumNum += v.num
    })
    if (state.result.length+1 >= sumNum) {
      // 抽奖结束
      isOver = true
    }

    return {
      ...state,
      nodes: nodes,
      isRun: false,
      result: [...state.result].concat(state.changeData),
      isOver: isOver,
    }
  },
  [CHOUJIANG_INVALID] : (state) => {
    // 无效的时候需要把中奖的号码放回中奖池
    let nodes = [...state.nodes]
    let ln = state.result.length
    let re = []

    if (ln > 0) {
      nodes.push(state.result[ln-1])
      re = _.slice(state.result, 0, ln-1)
    }

    return {
      ...state,
      nodes: nodes,
      changeData: "",
      result: re,
      isOver: false,
      err: null
    }
  },
}

// ------------------------------------
// Reducer
// ------------------------------------
const initialState = {
  // ca参与抽奖的数据
  nodes: config.nodes,
  // 变更记录
  changeData: "",
  // 结果记录
  result: [],
  // 是否在运行
  isRun: false,
  // 抽奖是否结束
  isOver: false,
  // 错误
  err: null,
  // 中奖配置信息
  config: _.omit(config, 'nodes')
}
export default function (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}
